import { login } from '@/api/login'
import { Message } from 'element-ui'
import router from '@/router'
import { getUserInfo } from '@/api/homePage'
// import { getToken, setToken, removeToken } from '@/utils/auth'
// import { resetRouter } from '@/router'

// const state = getDefaultState()

export default {
  namespaced: true,
  state() {
    return {
      token: '',
      userInfo: {}
    }
  },
  mutations: {
    // 登录保存token
    login(state, val) {
      state.token = val
    },
    // 退出登录
    logout(state) {
      state.token = ''
      router.push('/login')
    },
    // 登录首页保存用户信息
    getUserInfo(state, val) {
      state.userInfo = val
    }
  },
  actions: {
    // 登录
    async login(ctx, obj) {
      const res = await login(obj)
      console.log(res)
      if (res.code !== 200) return Message.error('登录失败')
      Message.success('登录成功')
      ctx.commit('login', res.data.token)
      router.push('/index')
    },
    // 获取用户信息
    async getUserInfo(ctx, obj) {
      const res = await getUserInfo()
      ctx.commit('getUserInfo', res.data)
    }
  }
}

